/**
XenSim simulates xenbal.
Copyright (C) 2006 Travis Vachon

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/
package xensim;


public class BalanceEventRI extends Event {
	private Node node;
	private SimGlobals globals;
	private Monitor monitor;
	
	public BalanceEventRI(double executionTime, SimGlobals globals, Monitor monitor,Node node){
		super(executionTime);
		this.node = node;
		this.globals = globals;
		this.monitor = monitor;
	}
	@Override
	public void execute() throws ConfigurationError{
		
		// check for migration possiblities on each node
		
		
		double load;
		
		load = monitor.getTotalLoad(node);
		//System.out.println(load);
		
		if (load > SimGlobals.UNLOADED_THRESHOLD) {
			//System.out.println(load);
			// choose a domain
			
			
			// if a domain is chosen, choose a destination
			Node chosenNode = null;
			
			
			double largest_load = SimGlobals.PEER_LOAD_THRESHOLD;
			
			for (Node node : globals.nodes) {
				load = monitor.getTotalLoad(node);
				//System.out.println(node.name + " " + node.numberOfDomains());
				//System.out.println("Load = " + load);
				if (!(this.node == node)
						&& !this.node.migrating
						&& !node.migrating
						&& load > SimGlobals.LOAD_THRESHOLD
						&& load > largest_load) {
					chosenNode = node;
					largest_load = load;
					
				}
			}
			if (chosenNode == null){
				for (Node node : globals.nodes) {
					if(!(this.node == node)
							&& node.domains.size() > this.node.domains.size() + 1){
						chosenNode = node;
					}
				}
			}
			
			Domain chosenDomain = null;
			if (chosenNode != null){
				largest_load = 0;

				for (Domain domain : node.domains) {
					if (monitor.getDomainLoad(domain) > largest_load
							&& domain.name != "dom0"
								&& !domain.migrating
								&& (domain.timeStartedOnNode < globals.currentTime - 
										(SimGlobals.MIN_NODE_TIME*SimGlobals.TIME_CONSTANT))) {
						//System.out.println("DomLoad: "+ monitor.getDomainLoad(domain));
						
						chosenDomain = domain;
					}
				}
			}
			if (chosenDomain != null && chosenNode != null) {
				globals.eventQueue.add(new MigrationEvent(
						executionTime, chosenNode, chosenDomain,
						this.node));
				System.out.println("migrating "+chosenDomain.name + " to " + this.node.name);
				
			}
			
		}
		
		
		
		globals.eventQueue.add(new BalanceEventRI(executionTime + SimGlobals.BALANCER_INTERVAL, this.globals,monitor,this.node));
		
		
	}
	
}
